import { _decorator, Component, input,  Input, Node, Vec3, Animation } from 'cc';
const { ccclass, property } = _decorator;

export const BLOCK_SIZE = 40;

@ccclass('playerContraller')
export class playerContraller extends Component {
    private _startJump:boolean = false;
    private _jumpStep:number = 0;
    private _curJumpTime:number = 0;
    private _jumpTime:number = 0.1;
    private _curJumpSpeed:number = 0;
    private _curPos:Vec3 = new Vec3();
    private _deltaPos :Vec3 = new Vec3();
    private _targetPos:Vec3 = new Vec3();
    private _curMoveIndex:number = 0;

    @property(Animation)
    BodyAnim:Animation = null;

    // @property(Node)
    // public leftTouch:Node|null = null;
    // @property(Node)
    // public rightTouch:Node|null = null;

    @property(Node)
    public GamePlant:Node = null;

    start() {
        // input.on(Input.EventType.MOUSE_UP,this.onMoveUp, this)
    }
    reset(){
        this._curMoveIndex = 0;
        this.node.setPosition(Vec3.ZERO);
        this._targetPos.set(0,0,0);
    }

    update(deltaTime: number) { //每帧更新
        if(this._startJump){
            this._curJumpTime += deltaTime //累计总的跳跃时间
            if(this._curJumpTime > this._jumpTime){ // 判断是否已经超过跳跃时间
                this.node.setPosition(this._targetPos) // 强制位置到终点
                this._startJump = false;
                this.onOnceJumpEnd();
            }else{
                this.node.getPosition(this._curPos);
                this._deltaPos.x = this._curJumpSpeed * deltaTime;
                Vec3.add(this._curPos, this._curPos, this._deltaPos );
                this.node.setPosition(this._curPos);
            }
        }
    }
    setInputActive(active: boolean) {
        if (active) {
            input.on(Input.EventType.MOUSE_UP, this.onMoveUp, this);
        } else {
            input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        }
    }

    onOnceJumpEnd(){
        this.node.emit('jumpEnd', this._curMoveIndex);
    }

    onMoveUp(event:MouseEvent){
        //按下左键
        if(event.getButton() === 0){
            this.jumpByStep(1)
        //按下右键
        }else if(event.getButton()===2){
            this.jumpByStep(2)
        }
    }

    jumpByStep(step:number){
    
        if (this._startJump) {
            return;
        }
        this._startJump = true;
        this._jumpStep = step;
        this._curJumpTime = 0;
    
        const clipName = step == 1 ? 'oneStep' : 'twoStep';
        const state = this.BodyAnim.getState(clipName);
        this._jumpTime = state.duration;
    
        this._curJumpSpeed = this._jumpStep * BLOCK_SIZE / this._jumpTime; //计算当前的跳跃速度
        this.node.getPosition(this._curPos) //获取角色的当前位置
        // console.log('curPos:',this._curPos)
        Vec3.add(this._targetPos, this._curPos, new Vec3(this._jumpStep * BLOCK_SIZE,0,0) ) //计算出目标位置

        if (this.BodyAnim) {
            if (step === 1) {
                this.BodyAnim.play('oneStep');
            } else if (step === 2) {
                this.BodyAnim.play('twoStep');
            }
        }

        this._curMoveIndex += step;

        
       
    }

}


